package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ibm.icu.util.Calendar;

import cn.com.api.entity.*;
import cn.com.api.service.ApiOrderService;
import cn.com.api.service.ApiT_memberService;
import cn.com.api.service.ApiT_member_entrepreneurService;
import cn.com.api.service.ApiT_member_idcardService;
import cn.com.api.service.ApiT_member_wemediaService;
import cn.com.api.service.ApiT_shipper_accountService;
import cn.com.api.service.ApiT_shippingService;
import cn.com.api.service.ApiT_statsService;
import cn.com.api.service.StartOnLoadService;
import cn.com.backstage.controller.base.T_memberBaseController;
import cn.com.util.*;

/*
 * 自媒体认证控制器
 */
@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_membermediaController {

	private static Logger logger = Logger.getLogger(T_membermediaController.class);

	@Autowired
	private ApiT_member_wemediaService apiT_member_wemediaService;
	@Autowired
	private ApiT_memberService apiT_memberService;

	/**
	 * 跳转用户自媒体认证信息界面
	 */
	@RequestMapping(value = "member_media_detail")
	public ModelAndView member_media_detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/member_media_detail.jsp");
		String userid = request.getParameter("userid");
		Map map = new HashMap();
		map.put("user_id", userid);
		List<ApiT_member_wemedia> list = apiT_member_wemediaService.findT_member_wemediasByMap(map);
		mv.addObject("mediadetail", list.get(0));
		return mv;
	}

	/**
	 * 后台审核企业主认证
	 */
	@RequestMapping(value = "validate_member_media")
	public void validate_member_media(HttpServletRequest request, HttpServletResponse response) throws Exception {
		BaseObj baseObj = new BaseObj();
		try {
			ApiT_user_admin user = (ApiT_user_admin) request.getSession().getAttribute("user_admin");
			String id = request.getParameter("id");
			String reason = request.getParameter("reason");
			String status = request.getParameter("status");

			// 跟新申请资料表
			ApiT_member_wemedia apiT_member_wemedia = apiT_member_wemediaService
					.findT_member_wemediaById(Integer.valueOf(id));
			apiT_member_wemedia.setUpdate_uid(user.getUser_id());
			apiT_member_wemedia.setUpdate_time(DateUtil.nowTimeToString());
			apiT_member_wemedia.setReason(reason);
			apiT_member_wemedia.setStatus(Integer.valueOf(status));
			apiT_member_wemediaService.updateT_member_wemedia(apiT_member_wemedia);

			// 跟新用户表状态
			ApiT_member apiT_member = apiT_memberService.findT_memberById(apiT_member_wemedia.getUser_id());
			if ("1".equals(status)) {// 通过
				apiT_member.setVerify_media(1);
			} else {// 驳回
				apiT_member.setVerify_media(3);
			}
			apiT_memberService.updateT_member(apiT_member);
			baseObj.handleSuccess();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			baseObj.handleException(e);
		}
		JsonUtil.printJson(response, JsonUtil.object2json(baseObj));
	}
	
	
	/**
	 * 企业主列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "member_wemedia-list")
	public ModelAndView member_wemedia_list(HttpServletRequest request, HttpServletResponse response, PageInfo pageInfo)
			throws Exception {
		ModelAndView mv = new ModelAndView("forward:/backstage/t_member_wemedia_list.jsp");

		Map<String, Object> conditions = new HashMap<String, Object>();
		List<Map<String, Object>> t_members = apiT_member_wemediaService.findMember_wemediasByMap(conditions);

		mv.addObject("member_wemedias", t_members);
		//
		return mv;
	}
	
}
